<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
error_reporting(0);
header('Content-Type: text/html; charset=utf-8');

class Admin_coupon extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->helper('url');
        $this->load->helper('form');
        $this->load->helper('html');
        $this->load->library('session');
        $this->load->model('users_model');
        $this->load->model('admin_product_model');
        $this->load->model('admin_category_model');
        $this->load->model('admin_promotion_model');
        $this->load->model('crud_model');
        $this->load->library('../controllers/crud');
        $this->load->library("pagination");
    }

    public function getTableCouponManager($start_from = 0)
    {
        $per_page = 4;
        $config = array();
        $config["base_url"] = base_url() . "/admin_coupon/getTableCouponManager/";
        $config["total_rows"] = $this->crud_model->selectNumOfPagination("*", "coupon AS co", "co.flag = 0 ORDER BY co.create_datetime DESC");
        $config["per_page"] = $per_page;
        $config["uri_segment"] = 3;
        $config['full_tag_open'] = '<ul>';
        $config['full_tag_close'] = '</ul>';
        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';
        $config['cur_tag_open'] = '<li class="active">';
        $config['cur_tag_close'] = '</li> ';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li> ';
        $config['next_link'] = 'Next';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['prev_link'] = 'Prev';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li> ';
        $choice = $config["total_rows"] / $config["per_page"];
        $config["num_links"] = round($choice);
        $config['use_page_numbers'] = TRUE;
        $start = $per_page * ($start_from-1);
        if($start < 0){
            $start = 0;
        }
        $data['couponList'] = $this->crud_model->selectPagination($per_page, $start, "*", "coupon AS co", "co.flag = 0 ORDER BY co.create_datetime DESC");
        $this->load->view('admin/coupon/coupon_list', $data);
    }

    public function getPage()
    {
        $result = $this->crud_model->selectNumOfPagination("*", "coupon AS co", "co.flag = 0 ORDER BY co.create_datetime DESC");
        $data['num_links'] = $result/4;
        $this->load->view('admin/coupon/page', $data);
    }

    public function insertNewCoupon(){
        $dt = date("Y-m-d H:i:s");

        $idUser = $this->crud->getIDUser();

        $couponCode = $this->input->post('couponCode', true);
        $couponName = $this->input->post('couponName', true);
        $discountTotal = $this->input->post('discountTotal', true);
        $discountStyle = $this->input->post('discountStyle', true);
        $conditionFrom = $this->input->post('conditionFrom', true);
        $conditionTo = $this->input->post('conditionTo', true);
        $expiredUsage = $this->input->post('expiredUsage', true);
        $minimumAmount = $this->input->post('minimumAmount', true);
        $usageTime = $this->input->post('usageTime', true);
        $couponStatus = $this->input->post('couponStatus', true);
        $detailApply = $this->input->post('detailApply', true);
        $detailStyle = $this->input->post('detailStyle', true);

        if($detailStyle == 1){
            $myArr = explode(",", $detailApply);
            $detailApply = "";
            foreach($myArr as $objMyArr){
                $id = $this->getIDProductByName($objMyArr);
                $detailApply .= $id.",";
            }
            $detailApply = substr($detailApply, 0, strlen($detailApply)-1);
        }

        $check = $this->admin_promotion_model->getInfoCouponCode($couponCode);
        if(!empty($check)){
            echo "Mã coupon đã tồn tại!";
        }
        else{
            $data = array(
                'coupon_code' => $couponCode,
                'coupon_name' => $couponName,
                'discount_total' => $discountTotal,
                'discount_style' => $discountStyle,
                'condition_from' => $conditionFrom,
                'condition_to' => $conditionTo,
                'expired_usage' => $expiredUsage,
                'minimum_amount' => $minimumAmount,
                'usage_time' => $usageTime,
                'coupon_status' => $couponStatus,
                'detail_apply' => $detailApply,
                'detail_style' => $detailStyle,
                'create_datetime' => $dt,
                'flag' => 0,
                'id_user' => $idUser
            );
            echo $this->crud_model->insert("coupon", $data);
        }
    }

    public function updateCoupon(){
        $dt = date("Y-m-d H:i:s");

        $id = $this->input->post('id', true);
        $couponName = $this->input->post('couponName', true);
        $discountTotal = $this->input->post('discountTotal', true);
        $discountStyle = $this->input->post('discountStyle', true);
        $conditionFrom = $this->input->post('conditionFrom', true);
        $conditionTo = $this->input->post('conditionTo', true);
        $expiredUsage = $this->input->post('expiredUsage', true);
        $minimumAmount = $this->input->post('minimumAmount', true);
        $usageTime = $this->input->post('usageTime', true);
        $couponStatus = $this->input->post('couponStatus', true);
        $detailApply = $this->input->post('detailApply', true);
        $detailStyle = $this->input->post('detailStyle', true);

        if($detailStyle == 1){
            $myArr = explode(",", $detailApply);
            $detailApply = "";
            foreach($myArr as $objMyArr){
                $idApply = $this->getIDProductByName($objMyArr);
                $detailApply .= $idApply.",";
            }
            $detailApply = substr($detailApply, 0, strlen($detailApply)-1);
        }

        $data = array(
            'coupon_name' => $couponName,
            'discount_total' => $discountTotal,
            'discount_style' => $discountStyle,
            'condition_from' => $conditionFrom,
            'condition_to' => $conditionTo,
            'expired_usage' => $expiredUsage,
            'minimum_amount' => $minimumAmount,
            'usage_time' => $usageTime,
            'coupon_status' => $couponStatus,
            'detail_apply' => $detailApply,
            'detail_style' => $detailStyle,
            'update_datetime' => $dt,
            'flag' => 0
        );

        echo $this->crud_model->update("coupon_id", $id, "coupon", $data);
    }

    public function deleteCoupon(){
        $id = $this->input->post('id');
        $query = $this->crud_model->deleteID('coupon_id', $id, "coupon");
        echo $query;
    }

    public function getAddParentCategory($id, $isProduct)
    {
//        echo $id;
        $data['info'] = $this->admin_category_model->getParentCategory($id);
        $data['idBefore'] = $id;
        $data['isProduct'] = $isProduct;
        $this->load->view('admin/coupon/add_category', $data);
    }

    public function getEditParentCategory($id, $isProduct)
    {
//        echo $id;
        $data['info'] = $this->admin_category_model->getParentCategory($id);
        $data['idBefore'] = $id;
        $data['isProduct'] = $isProduct;
        $this->load->view('admin/coupon/edit_category', $data);
    }

    public function getAddCategoryProduct()
    {
        $id_category = $this->input->post('idCate', true);
        if($id_category != 0){
            $result = $this->admin_product_model->getAddCategoryProduct($id_category);
        }
        else{
            $result = $this->admin_product_model->getAllCategoryProduct();
        }
        $string = "";
        foreach($result as $objResult){
            $string .= $objResult->product_name.", ";
        }
        echo $string;
    }

    public function getIDProductByName($productName){
        $field = "id";
        $id = $this->admin_product_model->getIDProductByName($field, $productName);
        return $id;
    }
}